#include "stdafx.h"
#include "CTextSetUI.h"
#include "ATW.h"
#include "Utilities.h"

const TCHAR* const kCloseButtonControlName = _T("closebtn");


CTextSetUI::CTextSetUI(HWND hParent /*= NULL*/, map<DString, DString>* p /*= NULL*/)
	: m_pTextPos(p)
	, m_pUnusedBtn(NULL)
	, m_pUsedBtn(NULL)
{
	Create(hParent, _T("TextSetUI"), UI_WNDSTYLE_FRAME | WS_POPUP, NULL, 0, 0, 0, 0);
}

CTextSetUI::~CTextSetUI()
{

}

LPCTSTR CTextSetUI::GetWindowClassName() const
{
	return _T("TextSetUI");
}

void CTextSetUI::OnFinalMessage(HWND hWnd)
{
	WindowImplBase::OnFinalMessage(hWnd);
}

void CTextSetUI::InitWindow()
{

}

DuiLib::DString CTextSetUI::GetSkinFile()
{
	return _T("cornertext.xml");
}

DuiLib::DString CTextSetUI::GetSkinFolder()
{
	return DString(CPaintManagerUI::GetInstancePath()) + _T("skin\\");
}

DuiLib::CControlUI* CTextSetUI::CreateControl(LPCTSTR pstrClass)
{
	return NULL;
}

LRESULT CTextSetUI::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	return __super::HandleMessage(uMsg, wParam, lParam);
}

LRESULT CTextSetUI::OnSysCommand(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
#if defined(WIN32) && !defined(UNDER_CE)
	BOOL bZoomed = ::IsZoomed(m_hWnd);
	LRESULT lRes = CWindowWnd::HandleMessage(uMsg, wParam, lParam);
	//if (::IsZoomed(m_hWnd) != bZoomed)
	//{
	//	if (!bZoomed)
	//	{
	//		CControlUI* pControl = static_cast<CControlUI*>(m_PaintManager.FindControl(kMaxButtonControlName));
	//		if (pControl) pControl->SetVisible(false);
	//		pControl = static_cast<CControlUI*>(m_PaintManager.FindControl(kRestoreButtonControlName));
	//		if (pControl) pControl->SetVisible(true);
	//	}
	//	else
	//	{
	//		CControlUI* pControl = static_cast<CControlUI*>(m_PaintManager.FindControl(kMaxButtonControlName));
	//		if (pControl) pControl->SetVisible(true);
	//		pControl = static_cast<CControlUI*>(m_PaintManager.FindControl(kRestoreButtonControlName));
	//		if (pControl) pControl->SetVisible(false);
	//	}
	//}
#else
	return __super::OnSysCommand(uMsg, wParam, lParam, bHandled);
#endif

	return 0;
}

void CTextSetUI::Notify(TNotifyUI& msg)
{
	if (msg.sType == _T("windowinit")) OnPrepare(msg);
	else if (msg.sType == _T("click")) 
	{
		DString name = msg.pSender->GetName();
		int idx = -1;
		list<COptionUI*>::iterator itrOpt;

		if (_tcsicmp(msg.pSender->GetName(), kCloseButtonControlName) == 0
			|| _tcsicmp(msg.pSender->GetName(), _T("btnsave")) == 0
			|| _tcsicmp(msg.pSender->GetName(), _T("btncancel")) == 0)
		{
			OnExit(msg);
		}
		else if (name == _T("lt_add"))
		{
			AddToPos(1);
		}
		else if (name == _T("lt_del"))
		{
			DeleteFromPos(1);
		}
		else if (name == _T("lt_delall"))
		{
			DeleteAll(1);
		}
		else if (name == _T("lt_up"))
		{
			Move(1, 1);
		}
		else if (name == _T("lt_down"))
		{
			Move(1, 0);
		}
		else if (name == _T("rt_add"))
		{
			AddToPos(2);
		}
		else if (name == _T("rt_del"))
		{
			DeleteFromPos(2);
		}
		else if (name == _T("rt_delall"))
		{
			DeleteAll(2);
		}
		else if (name == _T("rt_up"))
		{
			Move(2, 1);
		}
		else if (name == _T("rt_down"))
		{
			Move(2, 0);
		}
		else if (name == _T("lb_add"))
		{
			AddToPos(3);
		}
		else if (name == _T("lb_del"))
		{
			DeleteFromPos(3);
		}
		else if (name == _T("lb_delall"))
		{
			DeleteAll(3);
		}
		else if (name == _T("lb_up"))
		{
			Move(3, 1);
		}
		else if (name == _T("lb_down"))
		{
			Move(3, 0);
		}
		else if (name == _T("rb_add"))
		{
			AddToPos(4);
		}
		else if (name == _T("rb_del"))
		{
			DeleteFromPos(4);
		}
		else if (name == _T("rb_delall"))
		{
			DeleteAll(4);
		}
		else if (name == _T("rb_up"))
		{
			Move(4, 1);
		}
		else if (name == _T("rb_down"))
		{
			Move(4, 0);
		}
		else if (name == _T("btndefault"))
		{
			m_pTextPos->clear();

			m_pTextPos->insert(make_pair(_T("patname"), _T("1-0")));
			m_pTextPos->insert(make_pair(_T("agesex"), _T("1-1")));
			m_pTextPos->insert(make_pair(_T("patid"), _T("1-2")));
			m_pTextPos->insert(make_pair(_T("accnumber"), _T("1-3")));
			m_pTextPos->insert(make_pair(_T("wwwl"), _T("2-0")));
			m_pTextPos->insert(make_pair(_T("imgno"), _T("2-1")));
			m_pTextPos->insert(make_pair(_T("serno"), _T("2-2")));
			m_pTextPos->insert(make_pair(_T("flip"), _T("2-3")));
			m_pTextPos->insert(make_pair(_T("modality"), _T("3-0")));
			m_pTextPos->insert(make_pair(_T("patpos"), _T("3-1")));
			m_pTextPos->insert(make_pair(_T("scansequence"), _T("3-2")));
			m_pTextPos->insert(make_pair(_T("serdesc"), _T("3-3")));
			m_pTextPos->insert(make_pair(_T("studydesc"), _T("3-4")));
			m_pTextPos->insert(make_pair(_T("seriestime"), _T("3-5")));
			m_pTextPos->insert(make_pair(_T("acqtime"), _T("3-6")));
			m_pTextPos->insert(make_pair(_T("imagetime"), _T("3-7")));
			m_pTextPos->insert(make_pair(_T("hospital"), _T("3-8")));
			m_pTextPos->insert(make_pair(_T("zoom"), _T("4-0")));
			m_pTextPos->insert(make_pair(_T("trte"), _T("4-1")));
			m_pTextPos->insert(make_pair(_T("kvma"), _T("4-2")));
			m_pTextPos->insert(make_pair(_T("slicepos"), _T("4-3")));
			ParseConfig();
		}

	}
	else if (msg.sType == _T("selectchanged"))
	{
		DString name = msg.pSender->GetName();
		if (name.Find(_T('_')) != -1)
			m_pUsedBtn = static_cast<COptionUI*>(msg.pSender);
		else
			m_pUnusedBtn = static_cast<COptionUI*>(msg.pSender);

	}
	
}

void CTextSetUI::OnPrepare(TNotifyUI& msg)
{
	ParseConfig();
}

void CTextSetUI::OnExit(TNotifyUI& msg)
{
	if (_tcsicmp(msg.pSender->GetName(), _T("btnsave")) == 0) 
	{
		Close();
	}
	else
		Close(0);
}


void CTextSetUI::ParseConfig()
{
	if (m_pTextPos == NULL)
		return;

	list<COptionUI*> lstLT;
	list<COptionUI*> lstRT;
	list<COptionUI*> lstLB;
	list<COptionUI*> lstRB;

	m_pUnShow = static_cast<CVerticalLayoutUI*>(m_PaintManager.FindControl(_T("unshowitems")));

	map<DString, DString>::iterator itr = m_pTextPos->begin();
	for (; itr != m_pTextPos->end(); itr++)
	{
		vector<string> vec;
		vec = Split(TtoA(itr->second.GetData()), "-");
		if (vec.size() > 0)
		{
			COptionUI* pOpt = NULL;
			pOpt = static_cast<COptionUI*>(m_PaintManager.FindSubControlByName(m_pUnShow, itr->first));
			DString btnText = pOpt->GetText();

			int pos = atoi(vec.at(0).c_str());

			if (pos == 0)
			{
				if (pOpt) pOpt->SetVisible(true);
			}
			else
			{
				if (pOpt)
					pOpt->SetVisible(false);
				int idx = atoi(vec.at(1).c_str());

				list<COptionUI*>* pList = NULL;
				switch (pos)
				{
				case 1:
					pList = &lstLT;
					break;
				case 2:
					pList = &lstRT;
					break;
				case 3:
					pList = &lstLB;
					break;
				case 4:
					pList = &lstRB;
					break;
				}
				AddButton(itr->first, btnText, idx, pos, pList);
			}

		}
	}

	list<COptionUI*>::iterator itrOpt;
	m_pLT = static_cast<CVerticalLayoutUI*>(m_PaintManager.FindControl(_T("ltitems")));
	if (m_pLT)
	{
		m_pLT->RemoveAll();
		itrOpt = lstLT.begin();
		for (; itrOpt != lstLT.end(); itrOpt++)
		{
			m_pLT->Add((*itrOpt));
		}
	}

	m_pRT = static_cast<CVerticalLayoutUI*>(m_PaintManager.FindControl(_T("rtitems")));
	if (m_pRT)
	{
		m_pRT->RemoveAll();
		itrOpt = lstRT.begin();
		for (; itrOpt != lstRT.end(); itrOpt++)
		{
			m_pRT->Add((*itrOpt));
		}
	}

	m_pLB = static_cast<CVerticalLayoutUI*>(m_PaintManager.FindControl(_T("lbitems")));
	if (m_pLB)
	{
		m_pLB->RemoveAll();
		itrOpt = lstLB.begin();
		for (; itrOpt != lstLB.end(); itrOpt++)
		{
			m_pLB->Add((*itrOpt));
		}
	}

	m_pRB = static_cast<CVerticalLayoutUI*>(m_PaintManager.FindControl(_T("rbitems")));
	if (m_pRB)
	{
		m_pRB->RemoveAll();
		itrOpt = lstRB.begin();
		for (; itrOpt != lstRB.end(); itrOpt++)
		{
			m_pRB->Add((*itrOpt));
		}
	}
}

void CTextSetUI::AddButton(DString name, DString text, int idx, int pos, list<COptionUI*>* pList)
{
	list<COptionUI*>::iterator itrFind;
	DString prefix;

	switch (pos)
	{
	case 1:
		prefix = _T("lt_");
		break;
	case 2:
		prefix = _T("rt_");
		break;
	case 3:
		prefix = _T("lb_");
		break;
	case 4:
		prefix = _T("rb_");
		break;
	}

	itrFind = find_if(pList->begin(), pList->end(), FindFunc(name));
	if (itrFind == pList->end())
	{
		COptionUI* pBtn = new COptionUI();
		DString attr;
		DString btnName = prefix + name;
		attr.Format(_T("name=\"%s\" text=\"%s\" font=\"1\" align=\"left\" textpadding=\"4,0,0,0\" selectedtextcolor=\"#FFFFFFFF\" hotimage=\"itemhover.png\" selectedimage=\"itemselect.png\" group=\"gplt\""), btnName.GetData(), text.GetData());
		pBtn->SetAttributeList(attr);
		pBtn->SetTag(idx);
		pList->push_back(pBtn);
	}
	else
	{
		(*itrFind)->SetTag(idx);
	}

	pList->sort(SortFunc());
}

void CTextSetUI::AddToPos(int pos)
{
	if (m_pUnusedBtn)
	{
		m_pUnusedBtn->SetVisible(false);

		CVerticalLayoutUI* p = NULL;
		DString prefix;

		switch (pos)
		{
		case 1:
			p = m_pLT;
			prefix = _T("lt_");
			break;
		case 2:
			p = m_pRT;
			prefix = _T("rt_");
			break;
		case 3:
			p = m_pLB;
			prefix = _T("lb_");
			break;
		case 4:
			p = m_pRB;
			prefix = _T("rb_");
			break;
		}

		int idx = p->GetCount();

		COptionUI* pBtn = new COptionUI();
		DString attr;
		attr.Format(_T("name=\"%s%s\" text=\"%s\" font=\"1\" align=\"left\" textpadding=\"4,0,0,0\" selectedtextcolor=\"#FFFFFFFF\" hotimage=\"itemhover.png\" selectedimage=\"itemselect.png\" group=\"gplt\""),
			prefix.GetData(), m_pUnusedBtn->GetName().GetData(), m_pUnusedBtn->GetText().GetData());
		pBtn->SetAttributeList(attr);
		//pBtn->SetTag(idx);
		p->Add(pBtn);

		DString cfg;
		cfg.Format(_T("%d-%d"), pos, idx);
		map<DString, DString>::iterator itr = m_pTextPos->find(m_pUnusedBtn->GetName());
		itr->second = cfg;

		m_pUnusedBtn = NULL;
	}
}

void CTextSetUI::DeleteFromPos(int pos)
{
	if (m_pUsedBtn == NULL)
		return;

	CVerticalLayoutUI* p = NULL;
	DString prefix;

	switch (pos)
	{
	case 1:
		p = m_pLT;
		prefix = _T("lt_");
		break;
	case 2:
		p = m_pRT;
		prefix = _T("rt_");
		break;
	case 3:
		p = m_pLB;
		prefix = _T("lb_");
		break;
	case 4:
		p = m_pRB;
		prefix = _T("rb_");
		break;
	}

	if (m_pUsedBtn->GetName().Left(3) == prefix)
	{
		DString selName = m_pUsedBtn->GetName();
		p->Remove(m_pUsedBtn);

		selName.Replace(prefix, _T(""));
		COptionUI* pOpt = static_cast<COptionUI*>(m_PaintManager.FindSubControlByName(m_pUnShow, selName));
		if (pOpt)
		{
			pOpt->SetVisible(true);
			pOpt->Selected(false, false);
		}

		DString cfg;
		map<DString, DString>::iterator itr = m_pTextPos->find(selName);
		cfg = _T("0");
		itr->second = cfg;

		for (int i = 0; i < p->GetCount(); i++)
		{
			selName = p->GetItemAt(i)->GetName();
			selName.Replace(prefix, _T(""));
			//p->GetItemAt(i)->SetTag(i);
			cfg.Format(_T("%d-%d"), pos, i);
			itr = m_pTextPos->find(selName);
			itr->second = cfg;
		}

		m_pUsedBtn = NULL;
	}
}

void CTextSetUI::DeleteAll(int pos)
{
	CVerticalLayoutUI* p = NULL;
	DString prefix;
	switch (pos)
	{
	case 1:
		p = m_pLT;
		prefix = _T("lt_");
		break;
	case 2:
		p = m_pRT;
		prefix = _T("rt_");
		break;
	case 3:
		p = m_pLB;
		prefix = _T("lb_");
		break;
	case 4:
		p = m_pRB;
		prefix = _T("rb_");
		break;
	}

	for (int i = 0; i < p->GetCount(); i++)
	{
		DString usedName = p->GetItemAt(i)->GetName();
		usedName.Replace(prefix, _T(""));

		COptionUI* pOpt = static_cast<COptionUI*>(m_PaintManager.FindSubControlByName(m_pUnShow, usedName));
		if (pOpt)
		{
			pOpt->SetVisible(true);
			pOpt->Selected(false, false);
		}

		map<DString, DString>::iterator itrCfg = m_pTextPos->find(usedName);
		itrCfg->second = _T("0");
	}

	p->RemoveAll();
}

void CTextSetUI::Move(int pos, int updown)
{
	if (m_pUsedBtn == NULL)
		return;

	CVerticalLayoutUI* p = NULL;
	DString prefix;
	switch (pos)
	{
	case 1:
		p = m_pLT;
		prefix = _T("lt_");
		break;
	case 2:
		p = m_pRT;
		prefix = _T("rt_");
		break;
	case 3:
		p = m_pLB;
		prefix = _T("lb_");
		break;
	case 4:
		p = m_pRB;
		prefix = _T("rb_");
		break;
	}

	if (m_pUsedBtn->GetName().Left(3) != prefix)
		return;

	int nCount = p->GetCount();
	int idx = p->GetItemIndex(m_pUsedBtn);
	DString cfg;

	if (updown == 0)
	{
		if (idx == (nCount-1))
			return;

		p->Remove(m_pUsedBtn, true);
		p->AddAt(m_pUsedBtn, idx + 1);
	}
	else
	{
		if (idx == 0)
			return;

		p->Remove(m_pUsedBtn, true);
		p->AddAt(m_pUsedBtn, idx - 1);
	}

	for (int i=0; i<p->GetCount(); i++)
	{
		DString selName = p->GetItemAt(i)->GetName();
		selName.Replace(prefix, _T(""));
		cfg.Format(_T("%d-%d"), pos, i);
		map<DString, DString>::iterator itr = m_pTextPos->find(selName);
		itr->second = cfg;
	}

}
